package org.csu.mypetstore.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.mypetstore.api.common.CommonResponse;
import org.csu.mypetstore.api.entity.Item;
import org.csu.mypetstore.api.entity.LineItem;
import org.csu.mypetstore.api.entity.Order;
import org.csu.mypetstore.api.entity.OrderStatus;
import org.csu.mypetstore.api.persistence.ItemMapper;
import org.csu.mypetstore.api.persistence.LineItemMapper;
import org.csu.mypetstore.api.persistence.OrderMapper;
import org.csu.mypetstore.api.persistence.OrderStatusMapper;
import org.csu.mypetstore.api.service.OrderService;
import org.csu.mypetstore.api.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("orderService")
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderStatusMapper orderStatusMapper;
    @Autowired
    private LineItemMapper lineItemMapper;
    @Autowired
    private ItemMapper itemMapper;

    @Override
    public CommonResponse<List<OrderVO>> getOrdersByUserId(String userId) {
        QueryWrapper<Order> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("userid",userId);
        List<Order> orderList = orderMapper.selectList(queryWrapper);
        if(orderList.isEmpty()) {
            return CommonResponse.createForError("没有用户的订单信息");
        }
        List<OrderVO> orderVOList = new ArrayList<>();
        for(Order order : orderList){
            OrderVO orderVO = orderToOrderVO(order);
            orderVOList.add(orderVO);
        }
        return CommonResponse.createForSuccess(orderVOList);
    }

    @Override
    public CommonResponse<OrderVO> getOrdersByOrderId(String orderId) {
        Order order = orderMapper.selectById(orderId);
        if(order==null)
        {
            return CommonResponse.createForError("没有该订单号的订单信息");
        }
        OrderVO orderVO = orderToOrderVO(order);
        return CommonResponse.createForSuccess(orderVO);
    }

    private OrderVO orderToOrderVO(Order order) {
        OrderVO orderVO = new OrderVO();
        orderVO.setOrderId(order.getOrderId());
        orderVO.setUsername(order.getUsername());
        orderVO.setOrderDate(order.getOrderDate());
        orderVO.setShipAddress1(order.getShipAddress1());
        orderVO.setShipAddress2(order.getShipAddress2());
        orderVO.setShipCity(order.getShipCity());
        orderVO.setShipState(order.getShipState());
        orderVO.setShipZip(order.getShipZip());
        orderVO.setShipCountry(order.getShipCountry());
        orderVO.setBillAddress1(order.getBillAddress1());
        orderVO.setBillAddress2(order.getBillAddress2());
        orderVO.setBillCity(order.getBillCity());
        orderVO.setBillState(order.getBillState());
        orderVO.setBillZip(order.getBillZip());
        orderVO.setBillCountry(order.getBillCountry());
        orderVO.setCourier(order.getCourier());
        orderVO.setTotalPrice(order.getTotalPrice());
        orderVO.setBillToFirstName(order.getBillToFirstName());
        orderVO.setBillToLastName(order.getBillToLastName());
        orderVO.setShipToFirstName(order.getShipToFirstName());
        orderVO.setShipToLastName(order.getShipToLastName());
        orderVO.setCreditCard(order.getCreditCard());
        orderVO.setExpiryDate(order.getExpiryDate());
        orderVO.setCardType(order.getCardType());
        orderVO.setLocale(order.getLocale());

        OrderStatus orderStatus = orderStatusMapper.selectById(order.getOrderId());
        orderVO.setStatus(orderStatus.getStatus());

        QueryWrapper<LineItem> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("orderid",order.getOrderId());
        List<LineItem> lineItems = lineItemMapper.selectList(queryWrapper);
        orderVO.setLineItems(lineItems);

        return orderVO;
    }

    @Override
    public CommonResponse<OrderVO> addOrder(OrderVO orderVO) {
        Order order = new Order();
        order.setOrderId(orderVO.getOrderId());
        order.setOrderDate(orderVO.getOrderDate());
        order.setUsername(orderVO.getUsername());
        order.setShipAddress1(orderVO.getShipAddress1());
        order.setShipAddress2(orderVO.getShipAddress2());
        order.setShipCity(orderVO.getShipCity());
        order.setShipState(orderVO.getShipState());
        order.setShipZip(orderVO.getShipZip());
        order.setShipCountry(orderVO.getShipCountry());
        order.setBillAddress1(orderVO.getBillAddress1());
        order.setBillAddress2(orderVO.getBillAddress2());
        order.setBillCity(orderVO.getBillCity());
        order.setBillState(orderVO.getBillState());
        order.setBillZip(orderVO.getBillZip());
        order.setBillCountry(orderVO.getBillCountry());
        order.setCourier(orderVO.getCourier());
        order.setTotalPrice(orderVO.getTotalPrice());
        order.setBillToFirstName(orderVO.getBillToFirstName());
        order.setBillToLastName(orderVO.getBillToLastName());
        order.setShipToFirstName(orderVO.getShipToFirstName());
        order.setShipToLastName(orderVO.getShipToLastName());
        order.setCreditCard(orderVO.getCreditCard());
        order.setExpiryDate(orderVO.getExpiryDate());
        order.setCardType(orderVO.getCardType());
        order.setLocale(orderVO.getLocale());
        int insertOrder = orderMapper.insert(order);

        OrderStatus orderStatus = new OrderStatus();
        orderStatus.setStatus(orderVO.getStatus());
        orderStatus.setOrderId(orderVO.getOrderId());
        orderStatus.setLinenum(orderVO.getOrderId());
        orderStatus.setTimestamp(orderVO.getOrderDate());
        int insertOrderStatus = orderStatusMapper.insert(orderStatus);

        int insertLineItem = 0;
        for(int i=0;i<orderVO.getLineItems().size();i++) {
            LineItem lineItem = new LineItem();
            lineItem.setOrderId(orderVO.getOrderId());
            lineItem.setLineNumber(i+1);
            lineItem.setItemId(orderVO.getLineItems().get(i).getItemId());
            lineItem.setQuantity(orderVO.getLineItems().get(i).getQuantity());
            Item item = itemMapper.selectById(lineItem.getItemId());
            lineItem.setUnitPrice(item.getListPrice());
            insertLineItem = lineItemMapper.insert(lineItem);
            if(insertLineItem==0)
                break;
        }

        if(insertOrder==1&&insertOrderStatus==1&&insertLineItem==1) {
            return CommonResponse.createForSuccess("创建订单成功",orderVO);
        }else {
            return CommonResponse.createForError("创建订单失败");
        }
    }


    public CommonResponse<List<OrderVO>> searchOrderList(String keyword){
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.like("orderid",keyword);
        List<Order> orders = orderMapper.selectList(orderQueryWrapper);
        List<OrderVO> orderVOS = new ArrayList<>();
        for (Order order:orders){
            OrderVO orderVO = orderToOrderVO(order);
            orderVOS.add(orderVO);
        }
        return CommonResponse.createForSuccess(orderVOS);
    }

}
